#include <memory.h>
#include "merge_sort.h"

void merge_sort(char *c, int size, char *buffer){
	if(size <= 1){
		return;
	}
	int curArray, sizes[2] = {size / 2, size - size / 2};
	char *pBuffer = buffer, *pArrays[2] = {c, c + sizes[0]};
	merge_sort(pArrays[0], sizes[0], buffer);
	merge_sort(pArrays[1], sizes[1], buffer);
	while(sizes[0] != 0 || sizes[1] != 0){
		curArray = (*pArrays[0] < *pArrays[1]) ? (sizes[0] > 0 ? 0 : 1) : (sizes[1] > 0 ? 1 : 0);
		*(pBuffer++) = *(pArrays[curArray]++);
		sizes[curArray]--;
	}
	memcpy(c, buffer, size * sizeof(char));	
}
